package top.mhtools.pigeon.common.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author macheng
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enabled", havingValue = "true")
public class SwaggerConfig {

    /**
     * 基础数据API
     *
     * @return
     */
    @Bean
    public Docket bdcWebApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //api的相关描述信息，通常显示在页面的最上方
                .apiInfo(apiInfo("Pigeon", "Pigeon-API"))
                .select()
                //设置扫描的包
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.hxmec.pigeon.common.controller"))
                //设置扫描哪些controller，这里设置扫描全部，可以传入正则表达式
                .paths(PathSelectors.any())
                .build()
                .groupName("pigeon");
    }

    private ApiInfo apiInfo(String title, String desc) {
        return new ApiInfoBuilder()
                .title(title)
                .description(desc)
                .build();
    }


}
